home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJSRC106.ARJ / UPDATE.C < prev    next >
C/C++ Source or Header  |  1991-09-02  |  1KB  |  64 lines

  1. /* This file is intended to be compiled with Turbo-C */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5.  
  6. fatal(char *s)
  7. {
  8.   fputs(s, stderr);
  9.   exit(1);
  10. }
  11.  
  12. main(int argc, char **argv)
  13. {
  14.   int f1;
  15.   int f2;
  16.   char buf1[512];
  17.   char buf2[512];
  18.   int len1, len2;
  19.   
  20.   if (argc < 3)
  21.     fatal("Usage: udpate srcfile destfile\n");
  22.  
  23.   f1 = _open(argv[1], O_RDONLY);
  24.   if (f1 < 0)
  25.     fatal("Cannot open src file\n");
  26.  
  27.   f2 = _open(argv[2], O_RDONLY);
  28.  
  29.   if (f2 >= 0)
  30.     while (1)
  31.     {
  32.       len1 = _read(f1, buf1, 512);
  33.       len2 = _read(f2, buf2, 512);
  34.       if (len1 != len2)
  35.         break;
  36.       if (memcmp(buf1, buf2, len1))
  37.         break;
  38.       if (len1)
  39.         continue;
  40.       exit(0);
  41.     }
  42.  
  43.   if (f2 >= 0)
  44.   {
  45.     printf("File `%s' updated\n", argv[2]);
  46.     _close(f2);
  47.   }
  48.   else
  49.     printf("File `%s' created\n", argv[2]);
  50.  
  51.   lseek(f1, 0L, 0);
  52.   f2 = _creat(argv[2], 0);
  53.   while (1)
  54.   {
  55.     len1 = _read(f1, buf1, 512);
  56.     if (len1 == 0)
  57.       break;
  58.     len2 = _write(f2, buf1, len1);
  59.   }
  60.   _close(f1);
  61.   _close(f2);
  62.   exit(0);
  63. }
  64.